*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics


*** Figure 2: Effects of prior party identification on change in attitudes toward immigration
************************************************************************************************

use "DATA_wide.dta", clear
	
*** Left panel of Figure 2
	matrix w1w7 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w9 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w10 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w17 = J(9,3,.) // 8 rows, 3 columns 
	
	reg diff1_immi7 ib808.pi_b1
	estimate store m1
	mat r=r(table)
	mat list r(table)
	matrix w1w7[1, 1] = r[1,1]
	matrix w1w7[1, 2] = r[5,1]
	matrix w1w7[1, 3] = r[6,1]
	matrix w1w7[2, 1] = r[1,2]	
	matrix w1w7[2, 2] = r[5,2]
	matrix w1w7[2, 3] = r[6,2]
	matrix w1w7[3, 1] = r[1,3]
	matrix w1w7[3, 2] = r[5,3]
	matrix w1w7[3, 3] = r[6,3]
	matrix w1w7[4, 1] = r[1,4]
	matrix w1w7[4, 2] = r[5,4]
	matrix w1w7[4, 3] = r[6,4]
	matrix w1w7[5, 1] = r[1,5]
	matrix w1w7[5, 2] = r[5,5]
	matrix w1w7[5, 3] = r[6,5]
	matrix w1w7[6, 1] = r[1,6]
	matrix w1w7[6, 2] = r[5,6]
	matrix w1w7[6, 3] = r[6,6]
	matrix w1w7[7, 1] = r[1,7]
	matrix w1w7[7, 2] = r[5,7]
	matrix w1w7[7, 3] = r[6,7]
	matrix w1w7[8, 1] = r[1,8]
	matrix w1w7[8, 2] = r[5,8]
	matrix w1w7[8, 3] = r[6,8]
	matrix w1w7[9, 1] = .
	matrix w1w7[9, 2] = .
	matrix w1w7[9, 3] = .
	mat list w1w7

	reg diff7_immi9 ib808.pi_b7
	estimate store m2
	mat r=r(table)
	mat list r(table)
	matrix w7w9[1, 1] = r[1,1]
	matrix w7w9[1, 2] = r[5,1]
	matrix w7w9[1, 3] = r[6,1]
	matrix w7w9[2, 1] = r[1,2]	
	matrix w7w9[2, 2] = r[5,2]
	matrix w7w9[2, 3] = r[6,2]
	matrix w7w9[3, 1] = r[1,3]
	matrix w7w9[3, 2] = r[5,3]
	matrix w7w9[3, 3] = r[6,3]
	matrix w7w9[4, 1] = r[1,4]
	matrix w7w9[4, 2] = r[5,4]
	matrix w7w9[4, 3] = r[6,4]
	matrix w7w9[5, 1] = r[1,5]
	matrix w7w9[5, 2] = r[5,5]
	matrix w7w9[5, 3] = r[6,5]
	matrix w7w9[6, 1] = r[1,6]
	matrix w7w9[6, 2] = r[5,6]
	matrix w7w9[6, 3] = r[6,6]
	matrix w7w9[7, 1] = r[1,7]
	matrix w7w9[7, 2] = r[5,7]
	matrix w7w9[7, 3] = r[6,7]
	matrix w7w9[8, 1] = r[1,8]
	matrix w7w9[8, 2] = r[5,8]
	matrix w7w9[8, 3] = r[6,8]
	matrix w7w9[9, 1] = .
	matrix w7w9[9, 2] = .
	matrix w7w9[9, 3] = .
	mat list w7w9

	reg diff7_immi10 ib808.pi_b7
	estimate store m3
	mat r=r(table)
	mat list r(table)
	matrix w7w10[1, 1] = r[1,1]
	matrix w7w10[1, 2] = r[5,1]
	matrix w7w10[1, 3] = r[6,1]
	matrix w7w10[2, 1] = r[1,2]	
	matrix w7w10[2, 2] = r[5,2]
	matrix w7w10[2, 3] = r[6,2]
	matrix w7w10[3, 1] = r[1,3]
	matrix w7w10[3, 2] = r[5,3]
	matrix w7w10[3, 3] = r[6,3]
	matrix w7w10[4, 1] = r[1,4]
	matrix w7w10[4, 2] = r[5,4]
	matrix w7w10[4, 3] = r[6,4]
	matrix w7w10[5, 1] = r[1,5]
	matrix w7w10[5, 2] = r[5,5]
	matrix w7w10[5, 3] = r[6,5]
	matrix w7w10[6, 1] = r[1,6]
	matrix w7w10[6, 2] = r[5,6]
	matrix w7w10[6, 3] = r[6,6]
	matrix w7w10[7, 1] = r[1,7]
	matrix w7w10[7, 2] = r[5,7]
	matrix w7w10[7, 3] = r[6,7]
	matrix w7w10[8, 1] = r[1,8]
	matrix w7w10[8, 2] = r[5,8]
	matrix w7w10[8, 3] = r[6,8]
	matrix w7w10[8, 3] = r[6,8]
	matrix w7w10[9, 1] = .
	matrix w7w10[9, 2] = .
	matrix w7w10[9, 3] = .
	mat list w7w10
	
	reg diff7_immi17 ib808.pi_b7
	estimate store m4
	mat r=r(table)
	mat list r(table)
	matrix w7w17[1, 1] = r[1,1]
	matrix w7w17[1, 2] = r[5,1]
	matrix w7w17[1, 3] = r[6,1]
	matrix w7w17[2, 1] = r[1,2]	
	matrix w7w17[2, 2] = r[5,2]
	matrix w7w17[2, 3] = r[6,2]
	matrix w7w17[3, 1] = r[1,3]
	matrix w7w17[3, 2] = r[5,3]
	matrix w7w17[3, 3] = r[6,3]
	matrix w7w17[4, 1] = r[1,4]
	matrix w7w17[4, 2] = r[5,4]
	matrix w7w17[4, 3] = r[6,4]
	matrix w7w17[5, 1] = r[1,5]
	matrix w7w17[5, 2] = r[5,5]
	matrix w7w17[5, 3] = r[6,5]
	matrix w7w17[6, 1] = r[1,6]
	matrix w7w17[6, 2] = r[5,6]
	matrix w7w17[6, 3] = r[6,6]
	matrix w7w17[7, 1] = r[1,7]
	matrix w7w17[7, 2] = r[5,7]
	matrix w7w17[7, 3] = r[6,7]
	matrix w7w17[8, 1] = r[1,8]
	matrix w7w17[8, 2] = r[5,8]
	matrix w7w17[8, 3] = r[6,8]
	matrix w7w17[9, 1] = .
	matrix w7w17[9, 2] = .
	matrix w7w17[9, 3] = .
	mat list w7w17

*** Create left panel of Figure 2
	grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(w1w7[,1]), ci((w1w7[,2] w1w7[,3])) msymbol(circle) mcolor(gs0) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.25)) ///
			  (matrix(w7w9[,1]), ci((w7w9[,2] w7w9[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.08)) ///
			 (matrix(w7w10[,1]), ci((w7w10[,2] w7w10[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-.08)) ///
			 (matrix(w7w17[,1]), ci((w7w17[,2] w7w17[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.25)) ///
			, /// 
			coeflabels(r1="PI CDU" r2="PI CSU" r3="PI SPD" r4="PI FDP" r5="PI Greens" r6="PI Left" r7="PI AfD"  r8="PI Others" r9="No PI", labsize(large) labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(2 "w1-w4" 4 "w4-w5" 6 "w4-w6" 8 "w4-w10") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-6 0 -5 0)) ysize(2.5) xsize(3)  /// scale(.8)
			title("Coefficients", size(large)  color(gs0)) ///
			name(fig2a, replace)

			
			
*** Right panel of Figure 2
	matrix w1w7 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w9 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w10 = J(9,3,.) // 8 rows, 3 columns 
	matrix w7w17 = J(9,3,.) // 8 rows, 3 columns 
	
	estimate restore m1
	margins, at(pi_b1=(2 3 4 5 6 7 322 801 808))
	mat r=r(table)
	mat list r(table)
	matrix w1w7[1, 1] = r[1,1]
	matrix w1w7[1, 2] = r[5,1]
	matrix w1w7[1, 3] = r[6,1]
	matrix w1w7[2, 1] = r[1,2]	
	matrix w1w7[2, 2] = r[5,2]
	matrix w1w7[2, 3] = r[6,2]
	matrix w1w7[3, 1] = r[1,3]
	matrix w1w7[3, 2] = r[5,3]
	matrix w1w7[3, 3] = r[6,3]
	matrix w1w7[4, 1] = r[1,4]
	matrix w1w7[4, 2] = r[5,4]
	matrix w1w7[4, 3] = r[6,4]
	matrix w1w7[5, 1] = r[1,5]
	matrix w1w7[5, 2] = r[5,5]
	matrix w1w7[5, 3] = r[6,5]
	matrix w1w7[6, 1] = r[1,6]
	matrix w1w7[6, 2] = r[5,6]
	matrix w1w7[6, 3] = r[6,6]
	matrix w1w7[7, 1] = r[1,7]
	matrix w1w7[7, 2] = r[5,7]
	matrix w1w7[7, 3] = r[6,7]
	matrix w1w7[8, 1] = r[1,8]
	matrix w1w7[8, 2] = r[5,8]
	matrix w1w7[8, 3] = r[6,8]
	matrix w1w7[8, 1] = r[1,8]
	matrix w1w7[8, 2] = r[5,8]
	matrix w1w7[8, 3] = r[6,8]
	matrix w1w7[9, 1] = r[1,9]
	matrix w1w7[9, 2] = r[5,9]
	matrix w1w7[9, 3] = r[6,9]
	mat list w1w7
			
	estimate restore m2
	margins, at(pi_b7=(2 3 4 5 6 7 322 801 808))
	mat r=r(table)
	mat list r(table)
	matrix w7w9[1, 1] = r[1,1]
	matrix w7w9[1, 2] = r[5,1]
	matrix w7w9[1, 3] = r[6,1]
	matrix w7w9[2, 1] = r[1,2]	
	matrix w7w9[2, 2] = r[5,2]
	matrix w7w9[2, 3] = r[6,2]
	matrix w7w9[3, 1] = r[1,3]
	matrix w7w9[3, 2] = r[5,3]
	matrix w7w9[3, 3] = r[6,3]
	matrix w7w9[4, 1] = r[1,4]
	matrix w7w9[4, 2] = r[5,4]
	matrix w7w9[4, 3] = r[6,4]
	matrix w7w9[5, 1] = r[1,5]
	matrix w7w9[5, 2] = r[5,5]
	matrix w7w9[5, 3] = r[6,5]
	matrix w7w9[6, 1] = r[1,6]
	matrix w7w9[6, 2] = r[5,6]
	matrix w7w9[6, 3] = r[6,6]
	matrix w7w9[7, 1] = r[1,7]
	matrix w7w9[7, 2] = r[5,7]
	matrix w7w9[7, 3] = r[6,7]
	matrix w7w9[8, 1] = r[1,8]
	matrix w7w9[8, 2] = r[5,8]
	matrix w7w9[8, 3] = r[6,8]
	matrix w7w9[9, 1] = r[1,9]
	matrix w7w9[9, 2] = r[5,9]
	matrix w7w9[9, 3] = r[6,9]
	mat list w7w9

	estimate restore m3
	margins, at(pi_b7=(2 3 4 5 6 7 322 801 808))
	mat r=r(table)
	mat list r(table)
	matrix w7w10[1, 1] = r[1,1]
	matrix w7w10[1, 2] = r[5,1]
	matrix w7w10[1, 3] = r[6,1]
	matrix w7w10[2, 1] = r[1,2]	
	matrix w7w10[2, 2] = r[5,2]
	matrix w7w10[2, 3] = r[6,2]
	matrix w7w10[3, 1] = r[1,3]
	matrix w7w10[3, 2] = r[5,3]
	matrix w7w10[3, 3] = r[6,3]
	matrix w7w10[4, 1] = r[1,4]
	matrix w7w10[4, 2] = r[5,4]
	matrix w7w10[4, 3] = r[6,4]
	matrix w7w10[5, 1] = r[1,5]
	matrix w7w10[5, 2] = r[5,5]
	matrix w7w10[5, 3] = r[6,5]
	matrix w7w10[6, 1] = r[1,6]
	matrix w7w10[6, 2] = r[5,6]
	matrix w7w10[6, 3] = r[6,6]
	matrix w7w10[7, 1] = r[1,7]
	matrix w7w10[7, 2] = r[5,7]
	matrix w7w10[7, 3] = r[6,7]
	matrix w7w10[8, 1] = r[1,8]
	matrix w7w10[8, 2] = r[5,8]
	matrix w7w10[8, 3] = r[6,8]
	matrix w7w10[9, 1] = r[1,9]
	matrix w7w10[9, 2] = r[5,9]
	matrix w7w10[9, 3] = r[6,9]
	mat list w7w10
	
	estimate restore m4
	margins, at(pi_b7=(2 3 4 5 6 7 322 801 808))
	mat r=r(table)
	mat list r(table)
	matrix w7w17[1, 1] = r[1,1]
	matrix w7w17[1, 2] = r[5,1]
	matrix w7w17[1, 3] = r[6,1]
	matrix w7w17[2, 1] = r[1,2]	
	matrix w7w17[2, 2] = r[5,2]
	matrix w7w17[2, 3] = r[6,2]
	matrix w7w17[3, 1] = r[1,3]
	matrix w7w17[3, 2] = r[5,3]
	matrix w7w17[3, 3] = r[6,3]
	matrix w7w17[4, 1] = r[1,4]
	matrix w7w17[4, 2] = r[5,4]
	matrix w7w17[4, 3] = r[6,4]
	matrix w7w17[5, 1] = r[1,5]
	matrix w7w17[5, 2] = r[5,5]
	matrix w7w17[5, 3] = r[6,5]
	matrix w7w17[6, 1] = r[1,6]
	matrix w7w17[6, 2] = r[5,6]
	matrix w7w17[6, 3] = r[6,6]
	matrix w7w17[7, 1] = r[1,7]
	matrix w7w17[7, 2] = r[5,7]
	matrix w7w17[7, 3] = r[6,7]
	matrix w7w17[8, 1] = r[1,8]
	matrix w7w17[8, 2] = r[5,8]
	matrix w7w17[8, 3] = r[6,8]
	matrix w7w17[9, 1] = r[1,9]
	matrix w7w17[9, 2] = r[5,9]
	matrix w7w17[9, 3] = r[6,9]
	mat list w7w17

*** Create right panel of Figure 2
	grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(w1w7[,1]), ci((w1w7[,2] w1w7[,3])) msymbol(circle) mcolor(gs0) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.25)) ///
			  (matrix(w7w9[,1]), ci((w7w9[,2] w7w9[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.08)) ///
			 (matrix(w7w10[,1]), ci((w7w10[,2] w7w10[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-.08)) ///
			 (matrix(w7w17[,1]), ci((w7w17[,2] w7w17[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.25)) ///
			, /// 
			coeflabels(r1="PI CDU" r2="PI CSU" r3="PI SPD" r4="PI FDP" r5="PI Greens" r6="PI Left" r7="PI AfD"  r8="PI Others" r9="No PI", labsize(large) labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(2 "w1-w4" 4 "w4-w5" 6 "w4-w6" 8 "w4-w10") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-4 0 -5 0)) ysize(2.5) xsize(3)  /// scale(.8)
			title("Predicted change", size(large)  color(gs0)) ///
			name(fig2b, replace)
			
			
*** Appendix: Regression tables for Figure 2
***********************************************

		esttab m1 m2 m3 m4 using Tables_Figure2_`var'.rtf, replace /// 
					stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
					cells(b(star fmt(2)) se(par fmt(2) )) ///
					mtitles("w1-w4" 3 "w4-w5" 5 "w4-w6" 7 "w7-w10") ///
					drop(808.pi_b1 808.pi_b7) ///
					varlabels ///
						(2.pi_b1 "PI CDU (w1)" ///
						3.pi_b1 "PI CSU (w1)" ///
						4.pi_b1 "PI SPD (w1)" ///
						5.pi_b1 "PI FDP (w1)" ///
						6.pi_b1 "PI Greens (w1)" ///
						7.pi_b1 "PI Left (w1)" ///
						322.pi_b1 "PI AfD (w1)" ///
						801.pi_b1 "PI Others (w1)" ///
						2.pi_b7 "PI CDU (w4)" ///
						3.pi_b7 "PI CSU (w4)" ///
						4.pi_b7 "PI SPD (w4)" ///
						5.pi_b7 "PI FDP (w4)" ///
						6.pi_b7 "PI Greens (w4)" ///
						7.pi_b7 "PI Left (w4)" ///
						322.pi_b7 "PI AfD (w4)" ///
						801.pi_b7 "PI Others (w4)" ///
						_cons "Constant") ///
					title("Table: Regression results for change in attitudes toward immigration") ///
					addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. Reference is 'No PI' category. *: p<0,05, **: p<0,01, ***: p<0,001.") /// 
					varwidth(40) modelwidth(15)	
